//
//  OptionView.m
//  TestOrderView
//
//  Created by 戈王 on 15/3/2.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "OptionView.h"
#import "OptionBtn.h"

#define edge 10
#define margin 30

@interface OptionView ()

/**
 *  Santor 2015.5.29 判断是否是代码调用的点击
 */
@property (nonatomic, assign) BOOL isClickBySelf;

@end

@implementation OptionView

- (instancetype)initWithOptionArray:(NSArray *)optionArray {
    if (self = [super init]) {
        _optionArray = optionArray;
//        _selectedOptions = [NSMutableArray arrayWithCapacity:optionArray.count];
        [self setupOptionView];
    }
    return self;
}

+ (instancetype)optionViewWithArray:(NSArray *)optionArray {
    return [[self alloc] initWithOptionArray:optionArray];
}


- (void)setupOptionView
{
    CGFloat OptionBtnX = edge;
    self.height = 44;
    
    for (int i = 0; i < self.optionArray.count; i++) {
        
        NSString *optionStr = self.optionArray[i];
        
        OptionBtn *optionBtn = [[OptionBtn alloc] init];
        [optionBtn setTitle:optionStr forState:UIControlStateNormal];
        
        optionBtn.x = OptionBtnX;
        optionBtn.width = [optionStr sizeWithfont:optionBtn.titleLabel.font maxSize:CGSizeMake(CGFLOAT_MAX, 44)].width + 44;
        OptionBtnX = OptionBtnX + optionBtn.width + margin;
        
        [optionBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:optionBtn];
        
        if (i == 0) {
            [self optionBtnClick:optionBtn];
        } else if (i == self.optionArray.count - 1) {
            self.width = optionBtn.x + optionBtn.width + 10;
        }
    }
}

- (void)setOption:(NSString *)option
{
    _option = option;
    
    // 瞅瞅选项数组中是否有该选项
    if ([_optionArray containsObject:option]) {
        // 有的话遍历
        for (id subView in self.subviews) {
            if ([subView isKindOfClass:[OptionBtn class]]) {
                // 当轮到这个btn的时候点击
                OptionBtn *temOBtn = (OptionBtn *)subView;
                if ([temOBtn.titleLabel.text isEqualToString:option]) {
                    _isClickBySelf = YES;
                    [self optionBtnClick:temOBtn];
                    return;
                }
            }
        }
    }
}

- (void)optionBtnClick:(OptionBtn *)sender
{
    // Santor 2015.5.29 如果不是代码调用 才让代理执行方法
    if (_isClickBySelf) {
        _isClickBySelf = NO;
    }
    
    if (self.selectBtn == sender) {
        return;
    }
    
    sender.selected = YES;
    self.selectBtn.selected = NO;
    self.selectBtn = sender;
    _option = sender.titleLabel.text;
    
     [[NSNotificationCenter defaultCenter] postNotificationName:OptionViewDidChoosedOptionNotification object:nil userInfo:@{OptionViewCurrentOption :@[_option]}];
}

// 适合多选的代码
/*
- (void)setupOptionView {
    
    self.height = 55;
    CGFloat OptionBtnX = edge;
    
    for (int i = 0; i < self.optionArray.count; i++) {
        
        NSString *optionStr = self.optionArray[i];
        
        OptionBtn *optionBtn = [[OptionBtn alloc] init];
        [optionBtn setTitle:optionStr forState:UIControlStateNormal];
        
        optionBtn.x = OptionBtnX;
        optionBtn.width = [optionStr sizeWithfont:optionBtn.titleLabel.font maxSize:CGSizeMake(CGFLOAT_MAX, self.height)].width + 44;
        optionBtn.y = (self.height - optionBtn.height) * 0.5;
        
        OptionBtnX += optionBtn.width + margin;
        
        [optionBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:optionBtn];
    }
}

- (void)setSelectedOptions:(NSMutableArray *)selectedOptions {
    _selectedOptions = [selectedOptions mutableCopy];
    
    for (NSString *option in selectedOptions) {
        for (id subView in self.subviews) {
            if ([subView isKindOfClass:[OptionBtn class]]) {
                // 当轮到这个btn的时候点击
                OptionBtn *temOBtn = (OptionBtn *)subView;
                if ([temOBtn.titleLabel.text isEqualToString:option]) {
                    _isClickBySelf = YES;
                    [self optionBtnClick:temOBtn];
                }
            }
        }
    }
}

- (void)optionBtnClick:(OptionBtn *)sender {
    
    if (sender.isSelected && _selectedOptions.count == 1) {
        return ;
    }
    
    [sender setSelected:!sender.isSelected];
    
    if (_isClickBySelf) {
        _isClickBySelf = NO;
        
    } else {
        if (!sender.isSelected) {
            [self.selectedOptions removeObject:sender.currentTitle];
        } else {
            [self.selectedOptions addObject:sender.currentTitle];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:OptionViewDidChoosedOptionNotification object:nil userInfo:@{OptionViewCurrentOption : [self.selectedOptions mutableCopy]}];
    }
}
*/

@end
